//
//  ShopRecommendtViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/8/13.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "ShopRecommendViewController.h"
#import "ShopInfoModel.h"
#import "ShopCollectionViewCell.h"
#import "NetworkTool.h"

@interface ShopRecommendViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (strong, nonatomic) UICollectionView *goodsCollectionView;
@end

@implementation ShopRecommendViewController

- (void)viewWillAppear:(BOOL)animated
{
	[self getGoodsInfo];
}
- (void)viewDidLoad {
    [super viewDidLoad];
	[self buildUI];
	[self setupRefresh];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getGoodsInfo{
	[[NetworkTool sharedTool] requestWithName:@"获取首页推荐商城信息" URLString:APIRecommendGoods parameters:nil method:@"GET" callBack:^(id responseObject) {
		//NSError *error = nil;
		//_jobArray = [NSJSONSerialization JSONObjectWithData:responseObject[@"data"][@"recruit"] options:NSJSONReadingMutableContainers error:&error];
		NSArray *array = responseObject[@"data"][@"goods"];
		NSLog(@"商品信息%@", array);
		[self.goodsArray removeAllObjects];
		for (NSDictionary *dict in array)
		{
			ShopInfoModel *shopInfo = [[ShopInfoModel alloc] init];
			[shopInfo setValuesForKeysWithDictionary:dict];
			[self.goodsArray addObject:shopInfo];
		}
		if (self.goodsArray) {
			NSLog(@"商品信息是：%@", _goodsArray);
			[self.goodsCollectionView reloadData];
		}
		else
			NSLog(@"没有拿到商品信息");
	}];
}

- (void)buildUI
{
	NSLog(@"我到这里啦！！！！！");
	UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
	[layout setScrollDirection:UICollectionViewScrollDirectionVertical];
	layout.itemSize = CGSizeMake(SW(2)-20, 220);
	_goodsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-SH(5)) collectionViewLayout:layout];
	_goodsCollectionView.delegate = self;
	_goodsCollectionView.dataSource = self;
	_goodsCollectionView.scrollEnabled = YES;
	_goodsCollectionView.backgroundColor = [UIColor whiteColor];
	_goodsCollectionView.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-SH(5));
	[_goodsCollectionView registerNib:[UINib nibWithNibName:@"ShopCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ShopCollectionCell"];
	[self.view addSubview:_goodsCollectionView];
}

- (void)setupRefresh {
	NSLog(@"setupRefresh -- 下拉刷新");
	UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
	[refreshControl addTarget:self action:@selector(refreshClick:) forControlEvents:UIControlEventValueChanged];
	refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在刷新"];
	refreshControl.tintColor = [UIColor blackColor];
	[self.goodsCollectionView addSubview:refreshControl];
}
// 下拉刷新触发，在此获取数据
- (void)refreshClick:(UIRefreshControl *)refreshControl {
	[refreshControl beginRefreshing];
	[self getGoodsInfo];
	[refreshControl endRefreshing];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
	ShopCollectionViewCell *cell = [self.goodsCollectionView dequeueReusableCellWithReuseIdentifier:@"ShopCollectionCell" forIndexPath:indexPath];
	//cell.shopInfo = self.goodsArray[indexPath.row];
	return cell;
}


//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
	return 1;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
	return 1;
}

- (NSMutableArray *)goodsArray
{
	if (!_goodsArray) {
		_goodsArray = [[NSMutableArray alloc] init];
	}
	return _goodsArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
